package com.net.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.net.dao.ResourceDao;
import com.net.dao.SuperDao;
import com.net.model.Resource;
import com.net.model.Role;

@Component("resourceDao")
public class ResourceDaoImpl extends SuperDao implements ResourceDao {

	public boolean delete(Resource u) {
		Resource cust = (Resource) this.getHibernateTemplate().load(
				Resource.class, u.getId());
		if (cust != null) {
			cust.setRoles(null);
			this.getHibernateTemplate().delete(cust);
			return true;
		} else
			return false;
	}

	public List<Resource> getResources(final int currentPage,
			final int perPage, final String orderType, final String orderName) {
		HibernateCallback hc = new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				if (orderType == null || orderName == null) {
					return session.createQuery("from Resource").setFirstResult(
							currentPage * perPage).setMaxResults(perPage)
							.list();
				} else {
					System.out.println("from Resource order by " + orderName
							+ " " + orderType);
					return session.createQuery(
							"from Resource order by " + orderName + " "
									+ orderType).setFirstResult(
							currentPage * perPage).setMaxResults(perPage)
							.list();
				}
			}
		};

		System.out.println(getHibernateTemplate().executeFind(hc));
		return (List<Resource>) getHibernateTemplate().execute(hc);
	}

	public Resource loadResourceById(int id) {
		return (Resource) this.getHibernateTemplate().load(Resource.class, id);
	}

	public Resource loadResourceByName(String name) {
		List<Resource> resources = getHibernateTemplate().findByCriteria(
				DetachedCriteria.forClass(Resource.class).add(
						Restrictions.eq("name", name)));
		if (resources != null && resources.size() > 0) {
			return resources.get(0);
		} else
			return null;
	}

	public void save(Resource u) {
		getHibernateTemplate().save(u);

	}

	public void update(Resource resource) {
		System.out.println("update:"+resource.getValue());
		getHibernateTemplate().saveOrUpdate(resource);

	}

	public List<Resource> getResourcesByCondition(final int currentPage,
			final int perPage, final String condition) {
		HibernateCallback hc = new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String str = "from Resource r where " + condition;
				System.out.println(str);
				return session.createQuery(str).setFirstResult(
						currentPage * perPage).setMaxResults(perPage).list();
			}

		};
		return getHibernateTemplate().executeFind(hc);
	}

	public List<Resource> listAll() {
		HibernateCallback hc = new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from Resource").list();
			}

		};
		return getHibernateTemplate().executeFind(hc);

	}

	public int getCount() {
		HibernateCallback hc = new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from Resource").list();
			}

		};
		return getHibernateTemplate().executeFind(hc).size();
	}

}
